package com.hzx.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.hibernate.criterion.DetachedCriteria;

import com.hzx.entity.PageBean;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

public class BaseAction<T> extends ActionSupport implements ModelDriven<T>, SessionAware {
	protected final static String HOME = "home";
	protected final static String LOGIN = "login";
	protected final static String LIST = "list";
	protected final static String UPDATE = "update";
	protected final static String RESTORE = "restore";
	
	protected Map<String, Object> session;
	//PageBean 分页查询对象
	protected PageBean pageBean = new PageBean();
	//离线查询对象
	private DetachedCriteria criteria;

	// Struts框架设置值 是通过set方法
	public void setPage(int page) {
		// 设置页面传递的当前页数
		pageBean.setCurrentPage(page);
	}
	//page:2
	//limit:5
	public void setLimit(int limit) {
		// 设置当前查询多少个
		pageBean.setPageSize(limit);
	}

	protected T model;

	public BaseAction() {
		ParameterizedType genericSuperclass = (ParameterizedType) this.getClass().getGenericSuperclass();
		Type[] actualTypeArguments = genericSuperclass.getActualTypeArguments();
		Class clazz = (Class) actualTypeArguments[0];
		// 设置criteria 类型
		criteria = DetachedCriteria.forClass(clazz);
		pageBean.setCriteria(criteria);
		try {
			model = (T) clazz.newInstance();
		} catch (InstantiationException | IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 将java对象转换成json数据
	 * 
	 * @param obj
	 *            对象
	 * @param confingArr
	 *            排除一些属性不需要转换成JOSO数据
	 * @throws IOException
	 */
	public void objToJson(Object obj, String[] confingArr) throws IOException {
		// 排除一些属性不需要转换成JOSO数据
		JsonConfig config = new JsonConfig();
		config.setExcludes(confingArr);
		JSONObject jsonObj = JSONObject.fromObject(obj, config);
		// 向客户端发送JSON
		// 响应对象
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json;charset =utf-8");
		// 获取一个Write
		PrintWriter writer = response.getWriter();
		writer.print(jsonObj.toString());
	}

	/**
	 * 默认 转换成json数据 分页
	 * 
	 * @param obj
	 * @throws IOException
	 */
	public void objToJson(Object obj) throws IOException {
		// 排除一些属性不需要转换成JOSO数据
		JsonConfig config = new JsonConfig();
		config.setExcludes(new String[] { "currentPage", "pageSize", "criteria" });
		JSONObject jsonObj = JSONObject.fromObject(obj, config);
		// 向客户端发送JSON
		// 响应对象
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json;charset =utf-8");
		// 获取一个Write
		PrintWriter writer = response.getWriter();
		writer.print(jsonObj.toString());
	}

	/**
	 * // 将list集合转换成json数据
	 * 
	 * @param list
	 * @param confingArr
	 * @throws IOException
	 */

	public void listToJson(List list, String[] confingArr) throws IOException {
		// 排除一些属性不需要转换成JOSO数据
		JsonConfig config = new JsonConfig();
		config.setExcludes(confingArr);
		JSONArray jsonObj = JSONArray.fromObject(list, config);
		// 向客户端发送JSON
		// 响应对象
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json;charset =utf-8");
		// 获取一个Write
		PrintWriter writer = response.getWriter();
		writer.print(jsonObj.toString());
	}
	/**
	 * 
	 * @param str 向页面传递的值
	 */
	public void writeJsp(String str){
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		try {
			PrintWriter writer = response.getWriter();
			writer.print(str);
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@Override
	public T getModel() {
		return model;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;

	}

}
